package nxtController;
// 78.91.71.120
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

public class Client
	{
	Socket socket;
	DataInputStream input;
	
	public volatile float angle, xDistance, yDistance;
	public volatile int delay;
	
	public static void main(String[] args) throws UnknownHostException, IOException 
		{
		new Client();
		}
	
	public Client() throws UnknownHostException, IOException
		{
		//Provide the IP for the image processing computer(may be localhost)
		String ip = "127.0.0.1" ; // JOptionPane.showInputDialog("Server IP", "78.91.71.120");
		socket = new Socket(ip, 1337);
		input=new DataInputStream(socket.getInputStream());
		
		Thread t = new Thread()
			{
			@Override
			public void run()
				{
				try
					{
					while(true)
						{
						ByteBuffer buf = ByteBuffer.allocate(16);
						int read = 0;
						while(read < buf.capacity())
							read += input.read(buf.array(), read, buf.capacity()-read);
						angle = buf.getFloat();
						xDistance = buf.getFloat();
						yDistance = buf.getFloat();
						delay = buf.getInt();
					//System.out.println(angle+", "+xDistance+", "+yDistance); 
						}
					}
				catch (IOException e)
					{
					e.printStackTrace();
					System.out.println("dead");
					System.exit(0);
					}
				System.out.println("wtf");
				}
			};
		t.setDaemon(false);
		t.start();
		
		t = new Thread()
			{
			@Override
			public void run()
				{
				while(true)
					{
					try
						{
						sleep(66);
						}
					catch (InterruptedException e)
						{
						e.printStackTrace();
						}
					}
				}
			};
		t.setDaemon(true);
		t.start();
		}
	}
